package cn.elead.it.sso.system.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.gitee.elead.web.api.ApiResponses;
import com.gitee.elead.web.controller.SuperController;

import cn.elead.it.sso.system.model.Client;
import cn.elead.it.sso.system.service.IClientService;
import cn.hutool.core.util.IdUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


/**
 * <p>
 * 单点客户端相关接口
 * </p>
 *
 * @author luopeng
 */
@RestController
@Api(tags = { "Client" }, description = "单点客户端相关接口")
@RequestMapping(value = "/api/client", produces = MediaType.APPLICATION_JSON_VALUE)
public class ClientController extends SuperController{

    @Autowired
    private IClientService clientService;
    
    /**
     * 查询单点客户端信息
     * 
     * @param clientId  客户端id
     * @param clientName  客户端名称
     * @param clientDesc  客户端描述
     * @return List<Client>
     */    
     @ApiOperation("查询单点客户端信息")
     @GetMapping
     @ApiImplicitParams({ //
         @ApiImplicitParam(name = "clientId", value = "客户端id", paramType = "query"),//
         @ApiImplicitParam(name = "clientName", value = "客户端名称", paramType = "query"),//
         @ApiImplicitParam(name = "clientDesc", value = "客户端描述", paramType = "query")//
     }) //
     public ApiResponses<List<Client>> findClients(//
         @RequestParam(value = "clientId", required = false) String clientId,//客户端id
         @RequestParam(value = "clientName", required = false) String clientName,//客户端名称
         @RequestParam(value = "clientDesc", required = false) String clientDesc){//客户端描述
         
         return findList(clientId, clientName, clientDesc);
     }

     /**
     * 查询单点客户端信息集合
     * 
     * @param clientId  客户端id
     * @param clientName  客户端名称
     * @param clientDesc  客户端描述
     * @return List<Client>
     */ 
     @ApiOperation("查询单点客户端信息集合")
     @GetMapping("/list")
     @ApiImplicitParams({ //
          @ApiImplicitParam(name = "clientId", value = "客户端id", paramType = "query"),//
         @ApiImplicitParam(name = "clientName", value = "客户端名称", paramType = "query"),//
         @ApiImplicitParam(name = "clientDesc", value = "客户端描述", paramType = "query")//
     }) //
     public ApiResponses<List<Client>> findList(//
         @RequestParam(value = "clientId", required = false) String clientId,//客户端id
         @RequestParam(value = "clientName", required = false) String clientName,//客户端名称
         @RequestParam(value = "clientDesc", required = false) String clientDesc){//客户端描述
         
         return success(clientService.query() //查询
             .eq(ObjectUtils.isNotEmpty(clientId), Client::getClientId, clientId)//客户端id
             .eq(ObjectUtils.isNotEmpty(clientName), Client::getClientName, clientName)//客户端名称
             .eq(ObjectUtils.isNotEmpty(clientDesc), Client::getClientDesc, clientDesc)//客户端描述
             .list());
     }

     /**
     * 分页查询单点客户端信息
     * 
     * @param clientId  客户端id
     * @param clientName  客户端名称
     * @param clientDesc  客户端描述
     * @return IPage<Client>
     */ 
     @ApiOperation("分页查询单点客户端信息")
     @GetMapping("/list/page")
     @ApiImplicitParams({ //
         @ApiImplicitParam(name = "clientId", value = "客户端id", paramType = "query"),//
         @ApiImplicitParam(name = "clientName", value = "客户端名称", paramType = "query"),//
         @ApiImplicitParam(name = "clientDesc", value = "客户端描述", paramType = "query"),//
         @ApiImplicitParam(name = "current", value = "当前页", paramType = "query"), //
         @ApiImplicitParam(name = "limit", value = "每页大小(默认每页条目20,最大条目数100)", paramType = "query") }) //        
     public ApiResponses<IPage<Client>> findPageList(//
         @RequestParam(value = "clientId", required = false) String clientId,//客户端id
         @RequestParam(value = "clientName", required = false) String clientName,//客户端名称
         @RequestParam(value = "clientDesc", required = false) String clientDesc){//客户端描述
         
         return success(clientService.query() //分页查询
             .eq(ObjectUtils.isNotEmpty(clientId), Client::getClientId, clientId)//客户端id
             .eq(ObjectUtils.isNotEmpty(clientName), Client::getClientName, clientName)//客户端名称
             .eq(ObjectUtils.isNotEmpty(clientDesc), Client::getClientDesc, clientDesc)//客户端描述
             .page(this.getPage()));
     }    

     /**
     * 根据ID查询单点客户端
     * 
     * @param id  单点客户端Id
     * @return Client
     */ 
     @ApiOperation(value = "根据ID查询单点客户端")
     @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "单点客户端id", required = true, paramType = "path") })
     @GetMapping("/{id}")
     public ApiResponses<Client> findById(@PathVariable("id") String id) {
         return success(clientService.getById(id));
     }    

     /**
     * 添加单点客户端信息
     * 
     * @param Client  单点客户端
     * @return Client
     */ 
     @ApiOperation(value = "添加单点客户端信息")
     @PostMapping
     public ApiResponses<Void> create(@RequestBody Client Client) {
         Client.setId(StringUtils.EMPTY);
         Client.setClientSecret(IdUtil.simpleUUID());
         clientService.save(Client);
         return success(HttpStatus.CREATED);
     }

     /**
     * 修改单点客户端信息
     * 
     * @param id  单点客户端Id
     * @param Client  单点客户端
     * @return Client
     */ 
     @ApiOperation(value = "修改单点客户端信息")
     @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "单点客户端id", required = true, paramType = "path") })
     @PutMapping("/{id}")
     public ApiResponses<Void> update(@PathVariable("id") String id, @RequestBody Client Client) {
         Client.setId(id);
         clientService.updateById(Client);
         return success();
     } 

     /**
     * 删除单点客户端信息
     * 
     * @param id  单点客户端Id
     * @return 
     */ 
     @ApiOperation(value = "删除单点客户端信息")
     @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "单点客户端id", required = true, paramType = "path") })
     @DeleteMapping("/{id}")
     public ApiResponses<Void> delete(@PathVariable("id") String id) {
    	 clientService.removeById(id);
         return success(HttpStatus.NO_CONTENT);
     }
 }    

 